package org.kuan.lab.common.controller;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.kuan.lab.base.controller.BaseBean;
import org.kuan.lab.base.util.AppAttributes;
import org.kuan.lab.base.util.PageDateModel;
import org.kuan.lab.common.entity.Dict;
import org.kuan.lab.common.service.DictService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;


/**
 * Project: erp<br>
 * Description: 字典信息管理Backing Bean<br>
 * Copyright (c) 2012<br>
 * All Rights Reserved.<br>
 * @author <a href="mailto:gly420@163.com">GuanLuYong</a><br>
 * 
 * type: father,son
 * SYS_ID, 1系统标识，0非系统标识
 */
@Controller @Scope("view")
public class DictBean extends BaseBean<Dict> implements Serializable {
	private static final long serialVersionUID = 1L;

	@Resource private DictService dictService;
	
	private Map<String, String> types;
	
	@PostConstruct public void initDictBean() {
		types = new HashMap<String, String>(2);
		types.put("父类", AppAttributes.ERP_DICT_TYPE_PARENT);
		types.put("子类", AppAttributes.ERP_DICT_TYPE_CHILD);
	}
	
	public void initAddDict() {
		instance = new Dict();
		instance.setSysId(0);
		instance.setStatus(1l);
	}

	@Override
	public void search() {
		String prefix = "SELECT d ";
		String ttPrefix = "SELECT COUNT(d) ";
		StringBuilder suffix = new StringBuilder("FROM Dict d WHERE 1=1 ");
		String code = (String) queryMap.get("code");
		if(code != null && !"".equals(code)) {
			suffix.append("AND d.code LIKE '" + code + "%' ");
		}
		String name = (String) queryMap.get("name");
		if(name != null && !"".equals(name)) {
			suffix.append("AND d.name LIKE '%" + name + "%' ");
		}
		String type = (String) queryMap.get("type");
		if(type != null && !"".equals(type)) {
			suffix.append("AND d.type = '" + type + "' ");
		}
		String orderby = "ORDER BY d.code";
		lazyData = new PageDateModel<Dict>(dictService, prefix, ttPrefix, suffix.toString(), orderby, null);
	}

	@Override
	public void editEntity() throws Exception {
		super.editEntity();
		dictService.update(instance);
	}

	@Override
	public void addEntity() throws Exception {
		super.addEntity();
		dictService.insert(instance);
	}

	@Override
	public void removeEntity() throws Exception {
		// TODO Auto-generated method stub
		
	}
	// Getter & Setter
	public DictService getDictService() {
		return dictService;
	}

	public void setDictService(DictService dictService) {
		this.dictService = dictService;
	}

	public Map<String, String> getTypes() {
		return types;
	}

}
